package parkManage;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 车位信息
 * 
 * @author 伟❤志
 *
 */
public class PositionModel {

	private int id;// 车位号
	private int cost;// 费用
	private int isFree = 1;// 是否为空位
	private String carLicense;// 车位停车车牌号

	static Scanner scanner = new Scanner(System.in);

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getCost() {
		return cost;
	}

	public void setCost(int cost) {
		this.cost = cost;
	}

	public int getIsFree() {
		return isFree;
	}

	public void setIsFree(int isFree) {
		this.isFree = isFree;
	}

	public String getCarLicense() {
		return carLicense;
	}

	public void setCarLicense(String carLicense) {
		this.carLicense = carLicense;
	}

	/**
	 * 根据车位号找车位id
	 * 
	 * @param cars
	 * @param positions
	 * @param id
	 */
	public int selectPosition(int id) {
//		for (int i = ParkTest.positions.size() - 1; i >= 0; i++) {
//			if (id == ParkTest.positions.get(i).getId()) {
//				return i;
//			}
//		}
		for (int i = 0; i < ParkTest.positions.size(); i++) {
			if (id == ParkTest.positions.get(i).getId()) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 根据车牌号找车辆id
	 * 
	 * @param cars
	 * @param license
	 * @return
	 */
	public int car(ArrayList<CarModel> cars, String license) {
		for (int i = 0; i < cars.size(); i++) {
			if (license.equals(cars.get(i).getLicense())) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 计算费用
	 * 
	 * @param cars
	 * @param positions
	 * @param i
	 * @param license
	 */
	public void calculate(int i, String license) {//i是车位id

		PositionModel positionModel = new PositionModel();

		int carI = positionModel.car(ParkTest.cars, license);

		long startTime = ParkTest.cars.get(carI).getStartTime().getTime();
		long leaveTime = ParkTest.cars.get(carI).getLeaveTime().getTime();

		System.out.println("停车时间：" + Math.ceil((double) (leaveTime - startTime) / 1000 / 60) + "分钟");
		double x = Math.ceil((double) (leaveTime - startTime) / 1000 / 60 / 60);

		int cost = (int) x * 2;
		ParkTest.positions.get(i).setCost(cost);

	}

	/**
	 * 查询车位状态
	 * 
	 * @param cars
	 * @param positions
	 */
	public void parkLot() {
		PositionModel po = new PositionModel();

		System.out.println("请输入车位号：");
		int poId = Integer.parseInt(scanner.nextLine());

		System.out.println("车位号：" + poId);

		int fre = po.selectAllFree(poId);// -1表示不是空闲位置
		if (fre == -1) {
			int id = po.selectPosition(poId);
			System.out.println("车位状态：" + "已被使用");
			System.out.println("车牌号：" + ParkTest.positions.get(id).getCarLicense());

			return;
		}

		System.out.println("车位状态：" + "空位");
		System.out.println("车牌号：" + "无");
	}

	/**
	 * 遍历空闲车位
	 * 
	 * @param frees
	 * @param poId
	 * @return
	 */
	public int selectAllFree(int poId) {
		for (int i : ParkTest.frees) {
			if (poId == i)
				return i;
		}
		return -1;
	}

}
